package com.ccl.iot.dialogs;import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.ccl.iot.privates.BitmapBuilder;
import com.ccl.iot.template.ImageButtonView;
import com.ccl.iot.template.StandardDialog;
import com.ccl.iot.R;

public abstract class EditDialog extends StandardDialog {
	public static final int KTITLEBAR_PADDING = 5;
	
	private RelativeLayout iTitleBar = null;
	private ImageButtonView iBack = null;
	private TextView iTitle = null;
	private ImageButtonView iSave = null;
	
	private RelativeLayout  iView = null;
	
	private LinearLayout iContent = null;

	public EditDialog(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		super.onCreate(savedInstanceState);
		
		initialize();
	}
	
	private void initialize(){
		RelativeLayout.LayoutParams tParams;
		
		iView = new RelativeLayout(this.getContext());
		this.setContentView(iView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		
		{	/*	Title bar	*/
			iTitleBar = new RelativeLayout(this.getContext());
			iTitleBar.setId(R.id.title_bar);
			iTitleBar.setBackgroundColor(Color.DKGRAY);
			iTitleBar.setPadding(KTITLEBAR_PADDING, KTITLEBAR_PADDING, KTITLEBAR_PADDING, KTITLEBAR_PADDING);
			
			iBack = new ImageButtonView(this.getContext());
			iBack.setImageBitmap(BitmapBuilder.GetBitmap(this.getContext(), android.R.drawable.ic_menu_revert));
			tParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			tParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			tParams.addRule(RelativeLayout.CENTER_VERTICAL);
			iTitleBar.addView(iBack, tParams);
			
			iTitle = new TextView(this.getContext(), null, android.R.attr.textAppearanceMedium);
			iTitle.setTextColor(Color.WHITE);
			tParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			tParams.addRule(RelativeLayout.CENTER_IN_PARENT);
			iTitleBar.addView(iTitle, tParams);
			
			iSave = new ImageButtonView(this.getContext());
			iSave.setImageBitmap(BitmapBuilder.GetBitmap(this.getContext(), android.R.drawable.ic_menu_save));
			tParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			tParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			tParams.addRule(RelativeLayout.CENTER_VERTICAL);
			iTitleBar.addView(iSave, tParams);
			
			tParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			tParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			iView.addView(iTitleBar, tParams);
		}
		
		iContent = new LinearLayout(this.getContext());
		iContent.setOrientation(LinearLayout.HORIZONTAL);
		tParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		tParams.addRule(RelativeLayout.BELOW, iTitleBar.getId());
		iView.addView(iContent, tParams);

		handleButtonEvent();
	}

	@Override
	public void setTitle(CharSequence title) {
		if (iTitle != null){
			iTitle.setText(title);
		}
	}

	@Override
	public void setTitle(int titleId) {
		if (iTitle != null){
			iTitle.setText(titleId);
		}
	}
	
	protected abstract boolean onOK();
	
	private void handleButtonEvent(){
		if (iSave != null){
			iSave.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (onOK()){
						EditDialog.this.dismiss();
					}
				}
			});
		}
		
		if (iBack != null){
			iBack.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (onCancel()){
						EditDialog.this.dismiss();
					}
				}
			});
		}
	}
	
	private void handleDoneEvent(EditText aText){
		if (aText.getImeOptions() == EditorInfo.IME_ACTION_DONE){
			aText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
				@Override
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
					if (actionId == EditorInfo.IME_ACTION_DONE){
						if (onOK()){
							EditDialog.this.dismiss();
						}
					}
					
					return false;
				}
			});
		}		
	}
	
	protected void setContent(View aView, ViewGroup.LayoutParams aParams){
		if (aView != null && iContent != null){
			iContent.addView(aView, aParams);
			
			if (aView instanceof ViewGroup){
				ViewGroup tView = (ViewGroup) aView;
				
				if (tView.getChildCount() > 0){
					for (int i = tView.getChildCount() - 1; i >= 0; i--){
						if (tView.getChildAt(i) instanceof EditText){
							handleDoneEvent((EditText) tView.getChildAt(i));
						}
					}
				}
			}
		}
	}
	
	protected void setContent(int aViewID, ViewGroup.LayoutParams aParams){
		if (iContent != null){
			setContent(LayoutInflater.from(getContext()).inflate(aViewID, null), aParams);
		}
	}
}
